﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kunlun.Models;

namespace Kunlun.BusinessLogic {
	public class KunlunFavourableCode {
		public static List<FavourableCode> GetCodes(int uid) {
			KunLunEntities entities = new KunLunEntities();

			return entities.FavourableCode.Where(o => o.Uid == uid).ToList();
		}

		public static bool UseFacourableCode(int uid, string code, string pass,out string msg) {
			KunLunEntities entities = new KunLunEntities();

			FavourableCode co = entities.FavourableCode.FirstOrDefault(o => !o.IsUsed && o.F_Code == code);
			if (co == null || co.Password != pass) {
				msg = "该卡已经使用或者密码错误！";
				return false;
			}
			if (co.UseBeginDate > DateTime.Now || co.UseBeginEnd < DateTime.Now) {
				msg = "该卡不在使用日期中！";
				return false;
			}

			Consumer user = entities.Consumers.FirstOrDefault(o => o.ID == uid);
			user.Scoring += (int)(co.Price1 * 10);
			co.Uid = uid;
			co.IsUsed = true;
			entities.SaveChanges();
			msg = "";
			return true;
		}
	}
}
